<?php

namespace Admin\Models\app;

use Admin\Models\BaseModel;

class XhappStore extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iAppId;

    /**
     *
     * @var string
     */
    public $iAppCode;

    /**
     *
     * @var string
     */
    public $sAppName;

    /**
     *
     * @var string
     */
    public $mAppUpdateContent;

    /**
     *
     * @var string
     */
    public $sAppSmCoverUrl;

    /**
     *
     * @var string
     */
    public $sAppCoverUrl;

    /**
     *
     * @var string
     */
    public $sAppDownUrl;

    /**
     *
     * @var string
     */
    public $sAppPackage;

    /**
     *
     * @var string
     */
    public $sAppVersion;

    /**
     *
     * @var string
     */
    public $iAppSize;

    /**
     *
     * @var string
     */
    public $sAppMd5;

    /**
     *
     * @var integer
     */
    public $bAppHide;

    /**
     *
     * @var integer
     */
    public $bAppHistory;

    /**
     *
     * @var integer
     */
    public $bAppDown;

    /**
     *
     * @var integer
     */
    public $bAppEnable;

    /**
     *
     * @var integer
     */
    public $iAppType;

    /**
     *
     * @var integer
     */
    public $bAppApart;

    /**
     *
     * @var integer
     */
    public $iAppSystem;

    /**
     *
     * @var integer
     */
    public $iUpdateLevel;

    /**
     *
     * @var integer
     */
    public $bOutSchool;

    /**
     *
     * @var integer
     */
    public $iAppTarget;

    /**
     *
     * @var string
     */
    public $sRepairUrl;

    /**
     *
     * @var string
     */
    public $sPlistUrl;

    /**
     *
     * @var string
     */
    public $mReMark;

    /**
     *
     * @var string
     */
    public $sAppExtend1;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("'XHApp_Store'");
    }


    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHApp_Store';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhappStore[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhappStore
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据传入id，删除对应记录并返回删除结果
     *
     * @param $idArr
     * @return mixed
     *
     * create by ck 20160923
     */
    public static function deleteRecordById($idArr){
        return self::find(array(
            'conditions' => "iAppId IN ({idArr:array}) AND bDelete = 0",
            'bind'       => [
                'idArr'     => $idArr
            ]
        ))->delete();
    }

    /**
     * 根据id及数据将信息赋值，有id则覆盖该id记录
     * 如传入旧记录信息，则将新记录为空的相应信息改为旧记录信息
     *
     * @param $id
     * @param array $data
     * @param $oldApp
     * @return XhappStore
     *
     * create by ck 20160531
     * modify by ck 20160718
     * modify by ck 20160923    化简
     * modify by ck 20161101
     */
    public static function createRecord($id, $data = array(), $oldApp = false)
    {
        //$data = array_filter($data);
        $obj = $id ? self::findFirst($id) : new self();
        $obj->assign($data);
        $obj->bAppHistory = 0;
        if($oldApp) {
            $obj->sAppCoverUrl      or $obj->sAppCoverUrl = $oldApp->sAppCoverUrl;
            $obj->sAppSmCoverUrl    or $obj->sAppSmCoverUrl = $oldApp->sAppSmCoverUrl;
            $obj->iAppCode != ''    or $obj->iAppCode = $oldApp->iAppCode;
            $obj->iAppType != ''    or $obj->iAppType = $oldApp->iAppType;
            $obj->bAppHide != ''    or $obj->bAppHide = $oldApp->bAppHide;
            $obj->bAppApart != ''   or $obj->bAppApart = $oldApp->bAppApart;
            $obj->iAppSystem != ''  or $obj->iAppSystem = $oldApp->iAppSystem;
            $obj->bOutSchool != ''  or $obj->bOutSchool = $oldApp->bOutSchool;
            $obj->iAppTarget != ''  or $obj->iAppTarget = $oldApp->iAppTarget;
        }
        return $obj;
    }

    /**
     * 根据应用编号，包名，系统查询是否有记录
     * 有则返回model，否则返回false
     *
     * @param array $data
     * @return XhappStore
     *
     * create by ck 20160904
     * modify by ck 20161101
     */
    public static function getOldApp($data = array())
    {
//        $appCode  = $data['iAppCode'];
        $packName = $data['sAppPackage'];
        $appSys   = $data['iAppSystem'];
        return self::findFirst("sAppPackage = '{$packName}' AND iAppSystem = '{$appSys}' AND bAppHistory = 0 AND bDelete=0");
    }

    /**
     * 根据模型检查名称是否与以前相同，不同则返回错误
     *
     * @param $data
     * @return bool
     *
     * create by ck 20160531
     */
//    public static function checkAppName($data)
//    {
//        $appCode = $data['XHApp_Store,iAppCode'];
//        $appName = $data['XHApp_Store,sAppName'];
//        $checkApp = self::findFirst("iAppCode = '{$appCode}' AND bAppHistory = 0 AND bDelete = 0");
//        if(!$checkApp) return true;
//        if($checkApp->sAppName == $appName) return true;
//        $check = self::find("iAppCode = '{$appCode}' AND bAppHistory = 0 AND bDelete = 0");
//        if($check->count() == 1 && $check->toArray()[0]['iAppId'] == $data['XHApp_Store,iAppId']) return true;
//        //if(self::find("iAppCode = '{$appCode}' AND bAppHistory = 0 AND bDelete = 0")->count()==1) return true;
//        return false;
//    }

    /**
     * 根据模型检查改包名下是否有其他编号的记录，有则返回错误
     *
     * @param $data
     * @return bool
     *
     * create by ck 20160601
     * modify by ck 20161101
     */
    public static function checkPackageName($data)
    {
        $appCode  = $data['iAppCode'];
        $packName = $data['sAppPackage'];
        $checkApp = self::find("sAppPackage = '{$packName}' AND bAppHistory = 0 AND bDelete = 0");
        if(!$checkApp) return true;
        foreach($checkApp as $check){
            if($check->iAppCode != $appCode) {
                if($checkApp->count() == 1 && $check->iAppId = $data['iAppId']) return true;
                return false;
            }
        }
        return true;
//        if($checkApp[0]->iAppCode == $appCode) return true;
//        $check = self::find("sAppPackage = '{$packName}' AND bAppHistory = 0 AND bDelete = 0");
//        if($check->count() == 1 && $check->toArray()[0]['iAppId'] == $data['iAppId']) return true;          //修改自身
//        return false;
    }

    /**
     * 根据应用编号检查应用面向对象，1表示校外，0表示校内
     *
     * @param $appCode
     * @return int
     *
     * create by ck 20160907
     */
    public static function checkAppOutSchool($appCode)
    {
        $app = self::findFirst("iAppCode = '{$appCode}' AND bAppHistory = 0 AND bDelete = 0");
        return $app ? $app->bOutSchool : 0;
    }

    /**
     * 获取名称，包名，版本等信息用于保留应用
     *
     * @return mixed
     *
     * create by ck 20161018
     */
    public static function getInfoForRetain()
    {
        return self::find([
            'columns'       => 'sAppName,sAppPackage,sAppVersion,iAppType',
            'conditions'    => 'bDelete = 0 AND bAppHistory = 0 AND iAppSystem = 1'
        ])->toArray();
    }

    /**
     * 根据应用ID获取应用名及包名   --用于删除保留应用及防火墙
     *
     * @param $idArr
     * @return mixed
     *
     * create by ck 20161019
     * modify by ck 20161020    返回增加应用编号--用于删除书架
     */
    public static function getNameAndPackageAndCodeById($idArr)
    {
        return self::find([
            'columns'       => 'sAppName,sAppPackage,iAppCode',
            'conditions'    => 'iAppId IN ({idArr:array})',
            'bind'          => [
                'idArr'         => $idArr
            ]
        ])->toArray();
    }

}
